package common;

import common.listeners.RelationListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import math.Relation;

public class Relations {
	
	private static HashMap<String, Relation> rels = new HashMap<String, Relation>();
	private static ArrayList<RelationListener> _relationListeners = new ArrayList<RelationListener>();
        
        
        public static void addRelationListener(RelationListener listener) {
            _relationListeners.add(listener);
        }
        
        public static void removeRelationListener(RelationListener listener) {
            _relationListeners.remove(listener);
        }
        
        public static void removeRelation(String r) {
            rels.remove(r);
            for (RelationListener listener : _relationListeners) {
                listener.relationRemoved(rels.get(r));
            } 
        }
        
	public static void addRelation(Relation r) {
		if (!rels.containsKey(r.getId())) {
                    rels.put(r.getId(), r);
                    for (RelationListener listener : _relationListeners) {
                        listener.relationAdded(r);
                    }
                    System.out.println("New relation: " + r.getId());
                }
                else
                {
                    System.out.println("Relation `" + r.getId() +"` already exists.");
                }
	}
	
	public static Relation getRelation(String id) {
		
		return rels.get(id);
	}
        
        public static Collection<Relation> getRelations() {
            return rels.values();
        }

}
